import { _decorator, Component, Node, AudioSource, assert, AudioClip } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('engine/AudioController')
export class AudioController extends Component {

    @property(AudioSource)
    public _audioSource: AudioSource = null;

    private clip: AudioClip = null!;
    private loop: boolean;

    public static instance: AudioController


    onLoad() {
        AudioController.instance = this
        // 获取 AudioSource 组件
        const audioSource:AudioSource = this.node.getComponent(AudioSource)!;
        // 将组件赋到全局变量 _audioSource 中
        this._audioSource = audioSource;
    }


    play(clip: AudioClip, loop: boolean = true) {
        // 播放音乐
        this.loop = loop;
        this.clip = clip;
        this._audioSource.clip = clip
        this._audioSource.loop = loop
        this._audioSource.play();
    }

    pause() {
        // 暂停音乐
        this._audioSource.pause();
    }

    resume() {
        this.play(this.clip, this.loop)
    }

    playOneShot() {
        this._audioSource.playOneShot(this.clip, 1);
    }

    stop(){
        this._audioSource.stop()
    }


}

